Exploration of Reported Complaints

Here is a quick exploration of the temporal trends in call volume when grouped by complaint type.

Note that this is currently using an older version of the data which is not fully cleaned yet! All interpretations should be taken with extreme caution.

We start with a function just to make it easier to manually select different sets of complaints to view.

## Function to plot subset of complaints in a faceted plot
plot_complaint_volume <- function(data, vars) {
  
  plot_data <- data %>% filter(incident_complaint_reported_by_dispatch %in% vars) %>%
    mutate(mo_yr = dmy(paste0("01-", mo, "-", yr))) %>%
    group_by(mo_yr, incident_complaint_reported_by_dispatch) %>%
    count()
  
  cbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#CC79A7", "#F0E442", "#0072B2", "#D55E00")
  
  p <- ggplot(plot_data) + 
    geom_line(aes(x = mo_yr, y = n, color = incident_complaint_reported_by_dispatch), alpha = 1) + 
    geom_point(aes(x = mo_yr, y = n, color = incident_complaint_reported_by_dispatch), alpha = 0.7) +
    geom_vline(xintercept = dmy("02-01-2020"), linetype = "dashed", alpha = 0.8) +
    scale_color_manual(values = cbPalette) +
    facet_wrap(~incident_complaint_reported_by_dispatch, ncol = 1) +
    labs(x = "Date", y = "Monthly Incidents", color = "Complaint")
  
  return(p)
}

For now, let's look at the top 3 complaints by frequency (breathing problems, sick person, and falls).

## View top 3 complaints
top_complaints <- ems_full %>% 
  group_by(incident_complaint_reported_by_dispatch) %>% 
  summarize(n = n()) %>% 
  top_n(3, n)

complaints <- top_complaints$incident_complaint_reported_by_dispatch

## Plot
plot_complaint_volume(ems_full, vars = complaints) + 
  labs(title = "Trends in Top 3 Complaints: Charlottesville") + 
  theme(plot.title = element_text(hjust = 0.5), legend.position = "none")

Plotting without faceting quickly gets too busy, but converting to an interactive layout with plotly can help with this by allowing for highlighting. Here are the top 8 complaints plotted together for better comparison.

#vars <- complaints
top_complaints <- ems_full %>% 
  group_by(incident_complaint_reported_by_dispatch) %>% 
  summarize(n = n()) %>% 
  top_n(8, n)

vars <- top_complaints$incident_complaint_reported_by_dispatch

plot_data <- ems_full %>% filter(incident_complaint_reported_by_dispatch %in% vars) %>%
  mutate(mo_yr = dmy(paste0("01-", mo, "-", yr))) %>%
  group_by(mo_yr, incident_complaint_reported_by_dispatch) %>%
  count()

# cbPalette <- c("#E69F00", "#56B4E9", "#009E73", "#CC79A7", "#F0E442", "#0072B2", "#D55E00")

## Key to tell plotly what variable to highlight by
key <- highlight_key(plot_data, ~incident_complaint_reported_by_dispatch)

## Create ggplot
plt <- ggplot(key, aes(text = paste('Complaint', incident_complaint_reported_by_dispatch, sep = ": "))) +
  geom_line(aes(x = mo_yr, y = n, color = incident_complaint_reported_by_dispatch), alpha = 1) + 
  #geom_point(aes(x = mo_yr, y = n), alpha = 0.7) +
  geom_vline(xintercept = dmy("02-01-2020"), linetype = "dashed", alpha = 0.8) +
  scale_color_brewer(palette = "Set2") +
  labs(title = "Call Volume by Reported Complaint", x = "Date", y = "Incidents per month") +
  theme_classic() +
  theme(plot.title = element_text(hjust = 0.5))

## Convert to plotly and set highlight options
plt_highlight <- ggplotly(plt, tooltip = c("text")) %>% 
  highlight(on = "plotly_hover", off = "plotly_doubleclick") %>%
  layout(showlegend = FALSE)

plt_highlight